package com.boss.fileapp.dao.impl;

import com.boss.fileapp.consts.TableName;
import com.boss.fileapp.dao.IAgencySalaryDao;
import com.boss.fileapp.entity.AgencySalary;
import com.boss.framework.dao.BaseDaoImpl;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Repository
public class AgencySalaryDaoImpl extends BaseDaoImpl implements IAgencySalaryDao {

    @Override
    public List<AgencySalary> selectColumnName(String agencyId, List<String> salaryCodeList) {
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" select ")
                .append(" sba.salary_code, sba.salary_code columnName ")
                .append(" from ").append(TableName.SALARY_BASEDATA).append(" sba ");
//                .append(" left join ").append(TableName.AGENCY_SALARY).append(" asy ")
//                .append(" on ").append(" asy.salary_id = sba.id and asy.is_deleted = 0 and asy.agency_id = ? ");
//        params.add(agencyId);
        sb.append(" where ").append("  1=1 ");
        StringBuilder inSql = new StringBuilder();
        StringBuilder orderSql = new StringBuilder();
        if(!CollectionUtils.isEmpty(salaryCodeList)){
            inSql.append(" and sba.salary_code in ( ");
            orderSql.append(" order by field(sba.salary_code, ");
            for(String salaryCode : salaryCodeList){
                if(salaryCodeList.indexOf(salaryCode) != salaryCodeList.size() - 1){
                    inSql.append(" ?, ");
                    orderSql.append(" ?, ");
                }else{
                    inSql.append(" ?) ");
                    orderSql.append(" ?) ");
                }
            }
            sb.append(inSql);
            params.addAll(salaryCodeList);
            sb.append(orderSql);
            params.addAll(salaryCodeList);
        }
        return super.getjdbcTemplate().query(sb.toString(),params.toArray(), new BeanPropertyRowMapper<>(AgencySalary.class));
    }
}
